//
//  LGCareTemplateOtherCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/19.
//  照护计划-模版导入-其他Cell

#import "LGCareTemplateOtherCell.h"

@interface LGCareTemplateOtherCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *heightTitle;
/**   */
@property (nonatomic , strong)UITextField *heightTF;
/**   */
@property (nonatomic , strong)UILabel *heightUnit;
/**   */
@property (nonatomic , strong)UILabel *weightTitle;
/**   */
@property (nonatomic , strong)UITextField *weightTF;
/**   */
@property (nonatomic , strong)UILabel *weightUnit;
/**   */
@property (nonatomic , strong)LGCareTemplateItemView *xhdjbView;
/**   */
@property (nonatomic , strong)LGCareTemplateItemView *dbyxView;
/**   */
@property (nonatomic , strong)LGCareTemplateItemView *killipView;

@end

@implementation LGCareTemplateOtherCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
        __weak typeof(self) weakSelf = self;
        self.xhdjbView.itemViewSelect = ^(NSString * _Nonnull content) {
            if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(templateOtherSelect:key:)]){
                [weakSelf.delegate templateOtherSelect:content key:@"xiaohdjb"];
            }
        };
        self.dbyxView.itemViewSelect = ^(NSString * _Nonnull content) {
            if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(templateOtherSelect:key:)]){
                [weakSelf.delegate templateOtherSelect:content key:@"dabyx"];
            }
        };
        self.killipView.itemViewSelect = ^(NSString * _Nonnull content) {
            if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(templateOtherSelect:key:)]){
                [weakSelf.delegate templateOtherSelect:content key:@"killip"];
            }
        };
    }
    return  self;
}

-(void)setHeight:(NSString *)height{
    _height = height;
    self.heightTF.text = height;
}

-(void)setWeight:(NSString *)weight{
    _weight = weight;
    self.weightTF.text = weight;
}

-(void)setXiaohdjb:(NSString *)xiaohdjb{
    _xiaohdjb = xiaohdjb;
    self.xhdjbView.content = xiaohdjb;
}

-(void)setDabyx:(NSString *)dabyx{
    _dabyx = dabyx;
    self.dbyxView.content = dabyx;
}

-(void)setKillip:(NSString *)killip{
    _killip = killip;
    self.killipView.content = killip;
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *content = textField.text;
    if(content.length>5){
        content = [content substringToIndex:4];
    }
    textField.text = content;
    NSString *key = (textField == self.heightTF)?@"height":@"weight";
    if(self.delegate && [self.delegate respondsToSelector:@selector(templateOtherInput:key:)]){
        [self.delegate templateOtherInput:content key:key];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.heightTitle];
    [self.baseView addSubview:self.heightTF];
    [self.baseView addSubview:self.heightUnit];
    [self.baseView addSubview:self.weightTitle];
    [self.baseView addSubview:self.weightTF];
    [self.baseView addSubview:self.weightUnit];
    [self.baseView addSubview:self.xhdjbView];
    [self.baseView addSubview:self.dbyxView];
    [self.baseView addSubview:self.killipView];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.contentView).offset(-viewPix(16));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.bottom.equalTo(self.contentView).offset(-viewPix(8));
        make.height.equalTo(@(viewPix(332)));
    }];
    [self.heightTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(28));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.width.equalTo(@(viewPix(50)));
        make.height.equalTo(@(viewPix(23)));
    }];
    [self.heightTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(40));
        make.centerY.equalTo(self.heightTitle);
        make.width.equalTo(@(viewPix(150)));
        make.height.equalTo(@(viewPix(30)));
    }];
    [self.heightUnit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.centerY.equalTo(self.heightTitle);
    }];
    [self.weightTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.heightTitle.mas_bottom).offset(viewPix(16));
        make.left.width.height.equalTo(self.heightTitle);
    }];
    [self.weightTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.width.height.equalTo(self.heightTF);
        make.centerY.equalTo(self.weightTitle);
    }];
    [self.weightUnit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.heightUnit);
        make.centerY.equalTo(self.weightTitle);
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:12];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)heightTitle{
    if(!_heightTitle){
        _heightTitle = [UILabel lableWithText:@"身高" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _heightTitle;
}

-(UITextField *)heightTF{
    if(!_heightTF){
        _heightTF = [[UITextField alloc]init];
        _heightTF.tintColor = [UIColor colorWithString:@"#333333"];
        _heightTF.textColor = [UIColor colorWithString:@"#333333"];
        _heightTF.textAlignment = NSTextAlignmentRight;
        _heightTF.keyboardType = UIKeyboardTypeDecimalPad;
        _heightTF.placeholder = @"请输入";
        _heightTF.font = LGFont(16);
        [_heightTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _heightTF;
}

-(UILabel *)heightUnit{
    if(!_heightUnit){
        _heightUnit = [UILabel lableWithText:@"cm" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _heightUnit;
}

-(UILabel *)weightTitle{
    if(!_weightTitle){
        _weightTitle = [UILabel lableWithText:@"体重" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _weightTitle;
}
-(UITextField *)weightTF{
    if(!_weightTF){
        _weightTF = [[UITextField alloc]init];
        _weightTF.tintColor = [UIColor colorWithString:@"#333333"];
        _weightTF.textColor = [UIColor colorWithString:@"#333333"];
        _weightTF.textAlignment = NSTextAlignmentRight;
        _weightTF.keyboardType = UIKeyboardTypeDecimalPad;
        _weightTF.placeholder = @"请输入";
        _weightTF.font = LGFont(16);
        [_weightTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _weightTF;
}

-(UILabel *)weightUnit{
    if(!_weightUnit){
        _weightUnit = [UILabel lableWithText:@"kg" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _weightUnit;
}

-(LGCareTemplateItemView *)xhdjbView{
    if(!_xhdjbView){
        _xhdjbView = [[LGCareTemplateItemView alloc] initWithFrame:CGRectMake(0, viewPix(97), Screen_W-viewPix(32), viewPix(78))];
        _xhdjbView.title = @"是否有消化道疾病";
    }
    return _xhdjbView;
}

-(LGCareTemplateItemView *)dbyxView{
    if(!_dbyxView){
        _dbyxView = [[LGCareTemplateItemView alloc] initWithFrame:CGRectMake(0, kMaxY(self.xhdjbView.frame), Screen_W-viewPix(32), viewPix(78))];
        _dbyxView.title = @"出院时大便是否隐血";
    }
    return _dbyxView;
}

-(LGCareTemplateItemView *)killipView{
    if(!_killipView){
        _killipView = [[LGCareTemplateItemView alloc] initWithFrame:CGRectMake(0, kMaxY(self.dbyxView.frame), Screen_W-viewPix(32), viewPix(78))];
        _killipView.title = @"killip≥3级";
    }
    return _killipView;
}

@end


#pragma mark
#pragma mark ====> LGCareTemplateItemView

@interface LGCareTemplateItemView()

/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *yesButton;
/**   */
@property (nonatomic , strong)UIButton *noButton;

@end

@implementation LGCareTemplateItemView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.titleLabel];
        [self addSubview:self.yesButton];
        [self addSubview:self.noButton];
    }
    return self;
}


-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.yesButton.selected = [content isEqualToString:@"1"]?YES:NO;
    self.noButton.selected = [content isEqualToString:@"1"]?NO:YES;
}

-(void)itemButtonTouched:(UIButton *)sender{
    sender.selected = YES;
    NSString *content = @"0";
    if(sender == self.yesButton){
        content = @"1";
        self.noButton.selected = NO;
    }else{
        self.yesButton.selected = NO;
    }
    if(self.itemViewSelect){
        self.itemViewSelect(content);
    }
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(12), viewPix(8), Screen_W-viewPix(60), viewPix(21));
    }
    return _titleLabel;
}

-(UIButton *)yesButton{
    if(!_yesButton){
        _yesButton = [UIButton buttonWithTitle:@"是" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _yesButton.frame = CGRectMake(viewPix(12), viewPix(37), (Screen_W-viewPix(64))/2.0, viewPix(32));
        [_yesButton setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_yesButton setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        _yesButton.cornerRidus = viewPix(16);
        [_yesButton addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _yesButton;
}

-(UIButton *)noButton{
    if(!_noButton){
        _noButton = [UIButton buttonWithTitle:@"否" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _noButton.frame = CGRectMake(kMaxX(self.yesButton.frame)+viewPix(8), kMinY(self.yesButton.frame), (Screen_W-viewPix(64))/2.0, viewPix(32));
        [_noButton setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_noButton setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        _noButton.cornerRidus = viewPix(16);
        [_noButton addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _noButton;
}

@end
